<?php

class pupil {
	
	private $id = 0;
	
	private $lastname = NULL;
	
	private $name = NULL;
	
	private $secondname = NULL;
	
	private $group = NULL;
	
	function __construct($id, $data = NULL) {
		$this->set_id($id);
		
		if (isset($this->id)) {
			$this->set_pupil_name();
			$this->set_pupil_group();
		}
	}
	
	function set_id($id) {
		if (is_numeric($id)) {
			$this->id = $id;
		} else {
			throw new Exception("'$id' is invalid pupil id");
		}
	}
	
	/**
	 * Инициализация ФИО ученика
	 * @param int $id Идентификатор пользователя
	 */
	private function set_pupil_name() {
		global $DB;
		
		$sql = file_get_contents(__DIR__ . '/sql/get_user_name.sql');
		
		$stmt = $DB->prepare($sql);
		$stmt->bind_param('d', $this->id);
		$stmt->execute();
		$data = $stmt->get_result()->fetch_array(MYSQLI_ASSOC);
		
		$this->set_lastname($data['lastname']);
		$this->set_firstname($data['firstname']);
	}
	
	function set_lastname($lastname) {
		$this->lastname = $lastname;
	}
	
	function set_firstname($firstname) {
		$data = explode(' ', $firstname);
		$this->name = $data[0];
		$this->secondname = (isset($data[1])) ? $data[1] : NULL;
	}
	
	function set_pupil_group() {
		global $DB;
		
		$sql = file_get_contents(__DIR__ . '/sql/get_user_group.sql');
		
		$stmt = $DB->prepare($sql);
		$stmt->bind_param('d', $this->id);
		$stmt->execute();
		$data = $stmt->get_result()->fetch_array(MYSQLI_ASSOC);
		
		$this->group = $data['group'];
	}
	
	function get_id() {
		return $this->id;
	}
	
	function get_lastname() {
		return $this->lastname;
	}
	
	function get_name() {
		return $this->name;
	}
	
	function get_secondname() {
		return $this->secondname;
	}
	
	function get_group() {
		return $this->group;
	}
	
	function __destruct() {
		
	}
}

?>
